﻿using Decoder = nhCore.Modbus.Decoder;

namespace nhCore.db;

public class NDataType(string name)
{
    public const string con_2ByteInt = "int16";
    public const string con_2ByteUInt = "uint16";
    public const string con_4ByteInt = "int32";
    public const string con_4ByteFloat = "float";
    public const string con_4ByteFloat反序 = "floatrreverse";

    public string Name { get; set; } = name;

    /// <summary>
    /// 寄存器数，不是字节数
    /// </summary>
    public int RegCount { get; set; } = name switch
    {
        con_2ByteUInt => 1,
        con_4ByteInt => 2,
        con_4ByteFloat => 2,
        con_4ByteFloat反序 => 2,
        _ => 1,
    };

    public static Decoder GetDecoder(ElementConfig c)
    {
        Decoder decoder;
        switch (c.NDataType.Name)
        {
            case NDataType.con_4ByteInt:
                decoder = new nhCore.Modbus.Int32Decoder();
                break;
            case NDataType.con_4ByteFloat:
                decoder = new nhCore.Modbus.FloatDecoder();
                break;
            case NDataType.con_4ByteFloat反序:
                decoder = new nhCore.Modbus.FloatReverseDecoder();
                break;
            case NDataType.con_2ByteUInt:
                decoder = new nhCore.Modbus.Uint16Decoder();
                break;
            default:
                decoder = new nhCore.Modbus.WeatherDecoder();
                break;
        };
        return decoder;
    }
}
